var util = require('../../libs/util.js');
var app = getApp();
var sliderWidth = 120; // 需要设置slider的宽度，用于计算中间位置
var startX = 0, endX = 0, prevScrollLeft = 0, prevId = 0, prevIndex = 0;
var px = 0, py = 0, px2 = 0, py2 = 0, windowWidth = 0;
Page({
  data: {
    hasData: false,
    middle_height: "100vh",
    dd_style: "",
    warehouse: '全部',
    warehouseId: '',
    delivery_date: util.dateObjectformat(new Date(), 'yyyy-MM-dd'),
    delivery_date_text: "今日签到",
    hasDispatch:false,
    hasOrderSignin:false,
    hasArrivalWarehouse:false
  },
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh()
  },
  onReady: function (e) {
  },
  onShow: function (e) {
    var that = this;
  },
  onLoad: function (e) {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName +"--运力管理端" });
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          middle_height: res.windowHeight - 100,
          detail_height: res.windowHeight - 40
        });
        windowWidth = res.windowWidth;
      }
    });
    if (e.warehouseId) {
      that.setData({
        warehouse: e.warehouse,
        warehouseId: e.warehouseId
      });
    }
    this.loadData();
  },
  onShareAppMessage: function () {
    return {
      title: app.appName +'-司机签到管理',
      path: '/pages/driver/signin?warehouse=' + this.data.warehouse + '&warehouseId=' + this.data.warehouseId
    }
  },
  loadData: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    wx.request({
      url: app.server + '/wechat/wxapplet/driverSignin',
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Cookie':app.globalData.cookie
      },
      data: { 
        openid: app.globalData.openid,
        signin_date: that.data.delivery_date
      },
      success: function (res) {
        wx.hideLoading();
        console.log(res.data)
        if (res.data.success) {
          var dispatchData = res.data.data[0][0], signinData = res.data.data[1];
          that.setData({
            hasDispatch: true,
            hasOrderSignin: signinData.length>0?true:false,
            dispatchData: dispatchData,
            signinData:signinData,
            warehouse: dispatchData.abbreviation || dispatchData.wh_name,
            warehouseId: dispatchData.wh_id
          });
          for (var i = 0; i < signinData.length;i++){
            if (signinData[i].signin_type == 'orderSignin'){
              that.setData({
                hasOrderSignin: true
              });
            } else if (signinData[i].signin_type == 'arrivalWarehouseSignin') {
              that.setData({
                hasArrivalWarehouse: true
              });
            }
          }
        }else{
          that.setData({
            hasDispatch: false
          });
        }
      },
      fail: function (error) {
        wx.hideLoading();
        console.log(error);
        that.setData({
          hasDispatch: false
        })
      }
    });

  },
  orderSignin:function(){
    var that = this;
    wx.showLoading({
      title: '提交中...',
    });
    var dispatchData = this.data.dispatchData;
    util.postData("driverSignin", {
      signin_date: that.data.delivery_date, 
      driver: dispatchData.driver, 
      plate: dispatchData.plate, 
      warehouse: dispatchData.capacity_shipper
    }, function (res, error) {
      if (res.data.success) {
        wx.showToast({
          title: '出车签到成功',
          icon: 'success',
          duration: 2000,
          success: function () {
            wx.redirectTo({
              url: '/pages/driver/signin'
            })
          }
        })

      } else {
        wx.showToast({
          title: '出车签到失败',
          icon: 'success',
          duration: 2000
        })
      }
      wx.hideLoading();
    });
  },
  arrivalWarehouseSignin: function () {
    var that = this;
    wx.showModal({
      title: '到仓签到',
      content: '到仓签名是必须司机和车辆都已经到达才可以签到，为了更加精准的定位，请打开GPS。',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '获取坐标中...',
          });
          wx.getLocation({
            type: 'wgs84',
            success: function (res) {
              wx.hideLoading();
              var latitude = res.latitude
              var longitude = res.longitude
              wx.showLoading({
                title: '提交中...',
              });
              var dispatchData = that.data.dispatchData;
              util.postData("driverArrivalWarehouseSignin", {
                signin_date: that.data.delivery_date,
                driver: dispatchData.driver,
                plate: dispatchData.plate,
                warehouse: dispatchData.capacity_shipper,
                latitude: latitude,
                longitude: longitude
              }, function (res, error) {
                if (res.data.success) {
                  wx.showToast({
                    title: '到仓签到成功',
                    icon: 'success',
                    duration: 2000,
                    success: function () {
                      wx.redirectTo({
                        url: '/pages/driver/signin'
                      })
                    }
                  })

                } else {
                  // wx.showToast({
                  //   title: '未到达仓库',
                  //   icon: 'success',
                  //   duration: 2000
                  // });
                  wx.showModal({
                    content: res.data.data,
                    showCancel: false,
                    success: function (res) {}
                  });
                }
                wx.hideLoading();
              });
            }
          })
        } else if (res.cancel) {

        }
      }
    })
    
  },
  changeDeliveryDate: function (e) {
    this.setData({
      delivery_date: e.detail.value,
      delivery_date_text: e.detail.value
    });
    this.loadData();
  },
  changeWarehouse: function (e) {
    var that = this;
    wx.navigateTo({
      url: '/pages/schedule/monitor_warehouse'
    })
  },
  gotoHome: function () {
    wx.redirectTo({
      url: '/pages/driver/orderList'
    })
  },
  gotoDriverInfo: function () {
    wx.redirectTo({
      url: '/pages/driver/driverInfo'
    })
  },
  gotoCurrentOrder: function () {
    wx.redirectTo({
      url: '/pages/driver/currentOrder'
    })
  },
  gotoDriverSignin: function () {
    wx.redirectTo({
      url: '/pages/driver/signin'
    })
  },
  gotoHistoryOrder: function () {
    wx.redirectTo({
      url: '/pages/driver/historyOrder'
    })
  }
})
